package geppetto.phraseHMM.phraseExtraction.phraseAccepter;


import geppetto.cat.corpus.BilingualCorpus;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;

import java.util.LinkedList;
import java.util.List;



public class NoTerminalPunctuation extends AbstractPhraseAccepter {

	BilingualCorpus corpus;

	public NoTerminalPunctuation(BilingualCorpus corpus) {
		this.corpus = corpus;
	}

	public boolean acceptPhrase(int sentenceNumber, byte sentenceSource,
			int[] foreignSentence, int[] sourceSentence, int sourceStartPos,
			int sourceEndPos, int foreignStartPos, int foreignEndPos,
			double score) {
		int sourceLen = sourceEndPos - sourceStartPos;
		int foreignLen = foreignEndPos - foreignStartPos;
		if (sourceLen == 0 & foreignLen == 0 && isPunct(corpus.getSourceString(sourceSentence[0])) && isPunct(corpus.getForeignString(foreignSentence[0]))) {
			return true;
		}
		// Look for pucntuation in any phrase
		for (int sourcePos = sourceStartPos; sourcePos <= sourceEndPos; sourcePos++) {
			if (isPunct(corpus.getSourceString(sourceSentence[sourcePos]))) {
				//System.out.println("i got a punctuation :" + corpus.getSourceString(sourceSentence[sourcePos]));
				//System.out.println(corpus.getSourceSentenceString(sentenceNumber, sentenceSource));
				return false;
			}
		}
		for (int foreignPos = foreignStartPos; foreignPos <= foreignEndPos; foreignPos++) {
			if (isPunct(corpus.getForeignString(foreignSentence[foreignPos]))) {
				return false;
			}
		}
		return true;
	}
	

	@Override
	public boolean acceptPhrase(ExtractedPhrasePair epp) {
		int foreignEndPos = epp.getForeignEndPos();
		int foreignStartPos = epp.getForeignStartPos();
		int sourceEndPos = epp.getSourceEndPos();
		int sourceStartPos = epp.getSourceStartPos();
		int[] sourceSentence = epp.getSourceSentence();
		int[] foreignSentence = epp.getForeignSentence();
		int sourceLen = sourceEndPos - sourceStartPos;
		int foreignLen = foreignEndPos - foreignStartPos;
		if (sourceLen == 0 & foreignLen == 0 && isPunct(corpus.getSourceString(sourceSentence[0])) && isPunct(corpus.getForeignString(foreignSentence[0]))) {
			return true;
		}
		// Look for pucntuation in any phrase
		for (int sourcePos = sourceStartPos; sourcePos <= sourceEndPos; sourcePos++) {
			if (isPunct(corpus.getSourceString(sourceSentence[sourcePos]))) {
				//System.out.println("i got a punctuation :" + corpus.getSourceString(sourceSentence[sourcePos]));
				//System.out.println(corpus.getSourceSentenceString(sentenceNumber, sentenceSource));
				return false;
			}
		}
		for (int foreignPos = foreignStartPos; foreignPos <= foreignEndPos; foreignPos++) {
			if (isPunct(corpus.getForeignString(foreignSentence[foreignPos]))) {
				return false;
			}
		}
		return true;
	}


	/**
	 * Should be made better Should receive a file with all punctuation marks to
	 * use, language specific.
	 * 
	 * @param s
	 * @return
	 */
	public boolean isPunct(String s) {
		 if (s.equalsIgnoreCase(".")) {
			return true;
		} else if (s.equalsIgnoreCase("!")) {
			return true;
		} else if (s.equalsIgnoreCase("?")) {
			return true;
		} else if (s.equalsIgnoreCase(":")) {
			return true;
		} else if (s.equalsIgnoreCase(";")) {
			return true;
		} else {
			return false;
		}

	}

}